"""
备份乾科新能源数据库
"""

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

from config.constants import driver_path, chrome_path
from utils.browser_utils import init_chrome_browser


class Testbackup:
    # 初始化浏览器
    driver = init_chrome_browser(
        chrome_path=chrome_path,
        driver_path=driver_path,
        detach=True  # 保持浏览器打开
    )

    # 启动浏览器（脚本结束后不会自动关闭）
    driver.get("https://47.122.30.27:33023/5f40ca64")

    driver.maximize_window()
    #  点击高级
    driver.find_element(By.XPATH, "/html/body/div/div[2]/button[3]").click()
    # 进入
    driver.find_element(By.XPATH, "/html/body/div/div[3]/p[2]/a").click()
    #  输入用户名
    driver.find_element(By.XPATH, "/html/body/div[1]/div/div/aside[3]/div/div[2]/div[1]/div/input").send_keys(
        "8codkm1r")
    # 输入密码
    driver.find_element(By.XPATH, "/html/body/div[1]/div/div/aside[3]/div/div[2]/div[2]/div/input").send_keys(
        "69a377d2")
    # 点击登录
    driver.find_element(By.XPATH, "/html/body/div[1]/div/div/aside[3]/div/div[2]/div[5]/div/button").click()
    # 点击数据库
    try:
        backupButton = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "/html/body/section/aside/section/div[2]/div/div/div[1]/div/ul/li["
                                                  "4]/a/span"))
        )
        backupButton.click()
    except Exception as e:
        print("操作失败:", e)
    finally:
        print("进入数据库页面")
    #  点击备份
    try:
        radiobutton = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "/html/body/section/section/section/div[1]/div/div[2]/div["
                                                  "2]/div/div/div[2]/div/div[2]/div/div[3]/table/tbody/tr[2]/td["
                                                  "6]/div/div/div/span[1]"))
        )
        radiobutton.click()
    except Exception as e:
        print("操作失败:", e)
    finally:
        print("点击备份")
    # 点击下载
    try:
        radiobutton = WebDriverWait(driver, 15).until(
            EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/div/div[2]/div[2]/div/div[1]/div/div[2]/div["
                                                  "1]/div/div[2]/div/div[4]/div[2]/table/tbody/tr[1]/td["
                                                  "7]/div/div/div/span[2]"))
        )
        radiobutton.click()
        assert radiobutton.text == "下载"
        print("点击下载")
    except Exception as e:
        print("操作失败:", e)
        # 显示完整错误信息
        print(e)
    finally:
        print("下载完成")
